/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.beepmystuff;

import android.content.Context;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.google.beepmystuff.BmsClient.ShareWith;

/** Encapsulates the preferences used by the BeepMyStuff activities. */
public class SharedPreferences implements BmsClient.SessionStorage, Preferences {
  private final String keyNickname;
  private final String keyPassword;
  private final String keyShareWith;
  private final ShareWith shareWithDefault;
  private final String keySessionToken;
  private final String keyLastRunVersion;

  private final android.content.SharedPreferences sharedPreferences;

  /**
   * Constructs a Preferences object backed by the default shared preferences
   * for the given context.
   */
  public SharedPreferences(Context context) {
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    keyNickname = context.getResources().getString(R.string.pref_nickname);
    keyPassword = context.getResources().getString(R.string.pref_password);
    keyShareWith = context.getResources().getString(R.string.pref_share_with);
    shareWithDefault = ShareWith.valueOf(
        context.getResources().getString(R.string.share_with_entryvalue_default));
    keySessionToken = context.getResources().getString(R.string.pref_session_token);
    keyLastRunVersion = context.getResources().getString(R.string.pref_last_run_version);
  }

  /** Returns a given string preference, or a default value if not set. */
  private String getStringPreference(String key, String defaultValue) {
    return sharedPreferences.getString(key, defaultValue);
  }

  /** Sets a given string preference. */
  private void setStringPreference(String key, String value) {
    Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
  }

  @Override
  public String getNickname() {
    return getStringPreference(keyNickname, "");
  }

  @Override
  public void setNickname(String nickname) {
    setStringPreference(keyNickname, nickname);
  }

  @Override
  public String getPassword() {
    return getStringPreference(keyPassword, "");
  }

  @Override
  public void setPassword(String password) {
    setStringPreference(keyPassword, password);
  }

  @Override
  public ShareWith getShareWith() {
    return ShareWith.tryValueOf(getStringPreference(keyShareWith, ""), shareWithDefault);
  }

  @Override
  public void setShareWith(ShareWith shareWith) {
    setStringPreference(keyShareWith, shareWith.name());
  }

  @Override
  public String getLastRunVersion() {
    return getStringPreference(keyLastRunVersion, "");
  }

  @Override
  public void setLastRunVersion(String version) {
    setStringPreference(keyLastRunVersion, version);
  }

  @Override
  public String getSessionToken() {
    return getStringPreference(keySessionToken, null);
  }

  @Override
  public void setSessionToken(String sessionToken) {
    setStringPreference(keySessionToken, sessionToken);
  }

  /** Clears all preferences. */
  public void clear() {
    sharedPreferences.edit().clear().commit();
  }
}
